package ua.kharkov.knure.pereverziev.diploma.db.DAOServices;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ua.kharkov.knure.pereverziev.diploma.db.entity.Airport;
import ua.kharkov.knure.pereverziev.diploma.db.entity.Schedule;

public class ScheduleService extends CommonService<Schedule> {

	private final Logger log = LoggerFactory.getLogger(ScheduleService.class);

	public ScheduleService() {
		super(Schedule.class);
	}

	@SuppressWarnings("unchecked")
	public List<Schedule> getDeparturesSchedule(final Airport airport) {
		final Schedule schedule = new Schedule();
		schedule.setDeparture(airport);
		final Criteria criteria = getCriteria(schedule).add(
				Restrictions.eq("departure", airport)).addOrder(
				Order.desc("departureTime"));
		final List<Schedule> result = criteria.list();
		log.info("Found " + result.size() + " items for departure schedule.");
		return result;
	}

	public List<Schedule> getArrivalsSchedule(final Airport airport) {
		final Schedule schedule = new Schedule();
		schedule.setDeparture(airport);
		final Criteria criteria = getCriteria(schedule).add(
				Restrictions.eq("destination", airport)).addOrder(
				Order.desc("departureTime"));
		final List<Schedule> result = criteria.list();
		log.info("Found " + result.size() + " items for arrival schedule.");
		return result;
	}
}
